package  org.fantasy.common.db.util;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import  org.fantasy.common.util.StringUtil;

public class EntityUtil {
	private static Log log = LogFactory.getLog(EntityUtil.class);

	/**
	 * 把对象转换成指定的类型
	 * 
	 * @param obj
	 * @param desc
	 * @return
	 * @throws Exception
	 */
	private static Object convertTo(Object obj, Class desc) {
		if (obj == null)
			return null;
		Class cls = obj.getClass();
		if (cls.equals(desc))
			return obj;
		if (desc.equals(Double.class))
			return new Double(obj.toString());
		else if (desc.equals(Long.class))
			return new Double(obj.toString()).longValue();
		else if (desc.equals(Integer.class))
			return new Double(obj.toString()).intValue();
		else if (desc.equals(Short.class))
			return new Double(obj.toString()).shortValue();
		else if (desc.equals(Byte.class))
			return new Double(obj.toString()).byteValue();
		else if (desc.equals(java.util.Date.class)) {
			if (obj instanceof java.util.Date)
				return obj;
			else if (cls.equals(Long.class))
				return new java.sql.Date((Long) obj);
		} else if (desc.equals(java.sql.Date.class)) {
			if (cls.equals(java.util.Date.class))
				return new java.sql.Date(((java.util.Date) obj).getTime());
			else if (cls.equals(java.sql.Timestamp.class))
				return new java.sql.Date(((java.util.Date) obj).getTime());
			else if (cls.equals(Long.class))
				return new java.sql.Date((Long) obj);
		} else if (desc.equals(java.sql.Timestamp.class)) {
			if (cls.equals(java.util.Date.class))
				return new java.sql.Timestamp(((java.util.Date) obj).getTime());
			else if (cls.equals(java.sql.Date.class))
				return new java.sql.Timestamp(((java.sql.Date) obj).getTime());
			else if (cls.equals(Long.class))
				return new java.sql.Date((Long) obj);
		} else if (desc.equals(String.class))
			return obj.toString();
		return obj;
	}

	/**
	 * 根据属性名，得到其类上的set方法，首先匹配valCls一致的，未找到，则 遍历寻找名称及参数个数一致的。
	 * 
	 * @param cls
	 * @param property
	 * @param valCls
	 * @return
	 */
	@SuppressWarnings("all")
	public static Method getSetMethod(Class cls, String property, Class valCls) {
		String methodName = "set" + StringUtil.firstCharUpperCase(property);
		try {
			// 尝试直接寻找
			Method method = cls.getMethod(methodName, valCls);
			return method;
		} catch (Exception e) {
			Method[] methods = cls.getMethods();
			for (Method m : methods) {
				if (m.getName().equals(methodName) && m.getReturnType().equals(Void.TYPE) && m.getParameterTypes().length == 1) {
					return m;
				}
			}
			log.debug(e);
		}

		return null;
	}

	/**
	 * 设置某个对象的属性值，如name，则调用setName(Object value)方法 其中参数根据实际参数进行转换。
	 * 未设置成功返回false，设置成功返回true，不抛出异常
	 * 
	 * @param obj
	 * @param property
	 * @param value
	 * @return
	 */
	@SuppressWarnings("all")
	public static boolean setObjectProperty(Object obj, String property, Object value) {
		if (obj == null)
			return false;

		Class cls = obj.getClass();
		// 获取set方法
		Method method = getSetMethod(cls, property, value.getClass());
		if (method == null)
			return false;
		// 类型转换
		Object newValue = convertTo(value, method.getParameterTypes()[0]);
		// 执行
		try {
			method.invoke(obj, newValue);
			return true;
		} catch (Exception e) {
			log.debug(e);
			return false;
		}
	}

	public static void main(String[] args) {
		System.out.println(convertTo(convertTo("1260152046750", Long.class), java.util.Date.class).getClass());
	}
}
